/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;

import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.InstanceSpecification;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * 
 */
public class ChooseLinkAssociationDialog extends Dialog {
	
	/**
	 * 
	 */
	protected InstanceSpecification instanceSpecification, source, target;
	
	/**
	 * 
	 */
	protected Association definingAssociation;
	
	/**
	 * 
	 */
	protected Tree assocTree;
	
	
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * @param instanceSpecification 
	 * @param parent 
	 */
	public ChooseLinkAssociationDialog (Shell parent, InstanceSpecification instanceSpecification, InstanceSpecification source, InstanceSpecification target) {
		super (parent);
		this.instanceSpecification = instanceSpecification;
		this.source = source;
		this.target = target;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	protected Control createDialogArea(Composite parent){
		Composite comp=(Composite)super.createDialogArea(parent);
		assocTree = new Tree(comp, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
		
		// this set will contain the classifiers classifying the source and target instance specifications.
		HashSet<Classifier> setOfClassifiers = new HashSet<Classifier>();
		
		Iterator<Classifier> sourceIter = source.getClassifiers().iterator();
		while(sourceIter.hasNext()){
			setOfClassifiers.add(sourceIter.next());
		}
		
		Iterator<Classifier> targetIter = target.getClassifiers().iterator();
		while(targetIter.hasNext()){
			setOfClassifiers.add(targetIter.next());
		}
		
		// set of associations where source and target are involved
		HashSet<Association> setOfAssociations = new HashSet<Association>();
		Iterator<Classifier> classIter = setOfClassifiers.iterator();
		while(classIter.hasNext()){
			Classifier classifier = classIter.next();
			Iterator<Association> assocIter = classifier.getAssociations().iterator();
			while(assocIter.hasNext()){
				Association association = assocIter.next();
				EList endTypes = association.getEndTypes();
				
				// self association
				if(endTypes.size()==1) {
					if(target.getClassifiers().contains(endTypes.get(0)) && source.getClassifiers().contains(endTypes.get(0)))
						setOfAssociations.add(association);
				}
				
				else if(endTypes.size()==2){
					if((source.getClassifiers().contains(endTypes.get(0)) && target.getClassifiers().contains(endTypes.get(1))) ||
							(source.getClassifiers().contains(endTypes.get(1)) && target.getClassifiers().contains(endTypes.get(0)))) {
						setOfAssociations.add(association);
					}
				}
				
				// none binary associations are not handled
			}
		}
		

		
		// build tree item for each classifier of the instance specification owing this slot
		Iterator<Association> iter = setOfAssociations.iterator();
		while(iter.hasNext()){
			Association currentAssoc = iter.next();
			if(isAvailableSF(currentAssoc)){
				TreeItem item = new TreeItem(assocTree, SWT.NONE);
				item.setText(currentAssoc.getName());
				item.setData(currentAssoc);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentAssoc));
			}
		}
			
		assocTree.setLayoutData(new GridData(300, 300));
		
		assocTree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = assocTree.getSelection();
				if(selection[0].getData() instanceof Association){
					definingAssociation = (Association)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		
		return comp;
	}
	

	/**
	 * Returns the elements to import.
	 * 
	 * @return 
	 */
	public Association getResult(){
		return definingAssociation;
	}
	
	
	/**
	 * This method tests if the given structuralFeature is available for defining a slot or not.
	 * 
	 * @param SF 	 * @param association 
	 * 
	 * @return 
	 */
	protected boolean isAvailableSF(Association association){
		boolean result = true;
		Iterator<Classifier> iter = instanceSpecification.getClassifiers().iterator();
		while(iter.hasNext()){
			Classifier currentAssoc = iter.next();
			if(association.equals(currentAssoc))
				return false;
		}
		
		return result;
	}
	

}